
import datetime
from datetime import date
import time
from itertools import groupby
from operator import itemgetter

import math
from openerp import netsvc
from openerp import tools
from openerp.osv import fields, osv
from openerp.tools.translate import _

class hr_holidays_status(osv.osv):
    _inherit = "hr.holidays.status"
    _columns = {                   
                'paid_method' : fields.selection([('paid','Paid'),('un-paid','Un-paid')],'Paid/Un-Paid',required=True),
                'auto_create' :fields.boolean('Auto Create'),
                'number_days': fields.float('Number Days'),
                'is_national_holiday': fields.boolean('Is National Holiday'),
            }
    
    def generate_allocation_request(self, cr, uid, ids, context=None):
        hr_holidays_pool = self.pool.get('hr.holidays')
        hr_employee_pool = self.pool.get('hr.employee')
        wf_service = netsvc.LocalService("workflow")
        for status in self.browse(cr, uid, ids):
            if status.auto_create:
                if status.number_days > 0:
                    cr.execute('''
                        SELECT id
                        FROM hr_employee
                        WHERE id not in (SELECT employee_id FROM hr_holidays WHERE holiday_status_id = %s AND type = 'add')
                    '''%(status.id))
                    employee_ids = [x[0] for x in cr.fetchall()]
                    for employee in hr_employee_pool.browse(cr, uid, employee_ids):
                        vals = {
                            'name': status.name + ' - ' + employee.name,
                            'holiday_status_id': status.id,
                            'employee_id': employee.id, 
                            'type':'add',
                            'number_of_days_temp': status.number_days,
                        }
                        new_id = hr_holidays_pool.create(cr, uid, vals)
                        wf_service.trg_validate(uid, 'hr.holidays', new_id, 'confirm', cr)
                        wf_service.trg_validate(uid, 'hr.holidays', new_id, 'validate', cr)
                        wf_service.trg_validate(uid, 'hr.holidays', new_id, 'second_validate', cr) 
        return True
    
hr_holidays_status()
    